<?php class receive extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    
    function select_by_array($array){
       $this->db->where($array);
       $this->db->order_by('receive_id','desc');
       $query = $this->db->get('receive');       
       $result = $query->result(); 
       //echo $this->db->last_query();exit;
       return $result;
    }
    
    
    function select_s(){
    	$this->db->select('*');
        $this->db->select_sum('count');
        $this->db->group_by('version_id');
        $query = $this->db->get('receive');
        $result = $query->result(); 
        return $result;
    
    }
    function insert($arr){
       $result = $this->db->insert('receive',$arr);
        return $this->db->insert_id();
    }
    
    function getreceive_array($arr){
       $this->db->limit(1);
       $this->db->where($arr);
       $this->db->order_by("receive_id", "desc");
       $query = $this->db->get('receive');
       $result = $query->result(); 
       return $result;
    }
    
    function select($id,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where(array('receive_id'=>$id));
       }
       $this->db->order_by('receive_id desc');
       $query = $this->db->get('receive');
       $result = $query->result(); 
       return $result;
    }
    
    function getcount($id,$version_id=''){
    	$where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('receive');
        $result = $query->result(); 
        return $result;
    }
    
    function getlast($version_id,$id){
       $this->db->where(array('category_id'=>$version_id,'version_id'=>$id));
       $this->db->select_sum('count');
       $query = $this->db->get('receive');
       $result = $query->result(); 
       return $result;
    }
    
    function update($id){
       $this->version_id = $_POST['version_id'];
       $this->category_id = $_POST['category_id'];
       $this->mod_id = $_POST['mod_id'];
       $this->count = $_POST['count'];
       $this->codes = $_POST['codes'];
       $this->contacter = $_POST['contacter'];

       $result = $this->db->update('receive',$this,array('receive_id'=>$id));
       return $result;
    }
    
    function update_ar($array,$id){
       $result = $this->db->update('receive',$array,array('receive_id'=>$id));
       return $result;
    }
    
    
    function del($id){
       $result = $this->db->delete('receive',array('receive_id'=>$id));
       return $result;
    }
}